package com.ax.business.hemp.controller;

import java.util.Map;

import com.ax.business.common.service.MakeOrderService;
import com.ax.business.common.util.MapToBean;
import com.ax.business.hemp.domain.HempRegisteDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ax.business.common.util.AjaxRes;
import com.ax.business.common.util.Const;
import com.ax.business.common.util.PageUtils;
import com.ax.business.hemp.service.HempRegisteService;

@Controller
@ResponseBody
public class HempRegisteController {

	@Autowired
	HempRegisteService hempRegisteService;

	@Autowired
	MakeOrderService makeOrderService;
	/**
	 * 含麻登记(模块:含麻)
	 * 
	 * @param HempRegisteMast hempRegisteMast
	 *           
	 * @return SUCC
	 */
	//String businessCode,/*@RequestParam Map<String, Object> params*/HempRegisteMast hempRegisteMast,@RequestParam("parameter[]")String[] parameter
	@RequestMapping("/insertHemp")
	public AjaxRes insertHemp(AjaxRes ajaxRes, @RequestBody Map<String,Object> map){
		try {
			//todo seesion获取用户信息
			//生成含麻登记单号
			String businessCode = map.get("businessCode").toString();
			String hempRegisteSeq = makeOrderService.getMakeOrderSer(businessCode);
			this.hempRegisteService.insertHempSer(map,hempRegisteSeq);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	/**
	 * 含麻查询(模块:含麻)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@RequestMapping("/queryHemp")
	public AjaxRes queryHemp(AjaxRes ajaxRes,@RequestBody Map<String, Object> map){
		try {
			PageUtils hemps = this.hempRegisteService.queryHempSer(map);
			ajaxRes.setSucceed(hemps,Const.DATA_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 禁用启用含麻详细信息
	 * @param ajaxRes
	 * @param flag
	 * @param hempRegisteDetailId
	 * @return
	 */
	@RequestMapping("/updateHFlag")
	public AjaxRes updateFlag(AjaxRes ajaxRes,@RequestBody Map<String,Object> map){
		try{
			String[] hempRegisteDetailId = map.get("hempRegisteDetailId").toString().split(",");
			Integer flag = Integer.valueOf(map.get("flag").toString());
			Integer result = hempRegisteService.updateFlag(flag,hempRegisteDetailId);
			ajaxRes.setSucceed(result,Const.OPERATE_SUCCEED);
		}catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改含麻详细信息
	 * @param ajaxRes
	 * @param hempRegisteDetail
	 * @return
	 */
	@RequestMapping("/updateHempRegisteDetail")
	public AjaxRes updateHempRegisteDetail(AjaxRes ajaxRes,@RequestBody Map<String,Object> map){
		try{
			HempRegisteDetail hempRegisteDetail = MapToBean.toBean(HempRegisteDetail.class,map);
			Integer result = hempRegisteService.updateHempRegisteDetail(hempRegisteDetail);
			ajaxRes.setSucceed(result,Const.UPDATE_SUCCEED);
		}catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 删除含麻详细信息
	 * @param ajaxRes
	 * @param hempRegisteDetail
	 * @return
	 */
	@RequestMapping("/deleteHempRegisteDetail")
	public AjaxRes deleteHempRegisteDetail(AjaxRes ajaxRes,@RequestBody Map<String,Object> map){
		try{
			String[] hempRegisteDetailId  = map.get("hempRegisteDetailId").toString().split(",");
			Integer result = hempRegisteService.deleteHempRegisteDetail(hempRegisteDetailId);
			ajaxRes.setSucceed(result,Const.UPDATE_SUCCEED);
		}catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}



}
